# coding=utf-8


import smtplib
from email.mime.text import MIMEText

host = 'CNCAS3.mioffice.cn'
user = 'wupeng6@xiaomi.com'  # 发件人
password = 'Indoor123.'


def send_mail(to_list, subject, content):
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['from'] = user
    msg['to'] = ','.join(to_list)  # 注意，不是分号
    msg['subject'] = subject
    server = smtplib.SMTP()
    server.set_debuglevel(1)
    try:
        server.connect(host, 25)
    except:
        print('connect fail')

    try:
        server.ehlo(user)
        server.login(user, password)
        print('login ok')
    except:
        print('login fail')

    try:
        server.sendmail(user, to_list, str(msg))
        server.close()
        return True
    except OSError:
        print('OSError')
        return False
    except:
        print('unexpect error')
        return False


def test():
    if (send_mail(['wupeng6@xiaomi.com'], 'test', 'just test')):
        print('send ok')
    else:
        print('send fail')


if __name__ == '__main__':
    test()
